@page "/fss/task_group/add"
@using FOPS.Abstract.Fss.Server
@using FOPS.Abstract.Fss.Entity
@using FS.Utils.Common
@using FS.Utils.Component
@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;
@inject Blazored.LocalStorage.ILocalStorageService storage;

<div class="layui-row layui-col-space15">
    <div class="layui-col-md6">
        <div class="layui-card">
            <div class="layui-card-header">
                添加任务组
                <div class="layui-right">
                    <button class="layui-btn" @onclick="GotoList"><i class="am-icon-plus"></i> 列表</button>
                </div>
            </div>
            <div class="layui-card-body layui-row layui-col-space10">
                <div class="layui-form-item">
                    <label class="layui-form-label">任务组标题</label>
                    <div class="layui-input-block">
                        <input type="text" @bind="_info.Caption" autocomplete="off" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">Job名称</label>
                    <div class="layui-input-block">
                        <input type="text" @bind="_info.JobName" autocomplete="off" placeholder="客户端识别能处理的JOB" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-inline">
                        <input type="text" @bind="_info.StartAt" @bind:format="yyyy-MM-dd HH:mm:ss" lay-verify="date" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">时间间隔</label>
                        <div class="layui-input-inline">
                            <input type="text" @bind="_info.Cron" autocomplete="off" placeholder="请输入数字ms或cron" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">是否开启</label>
                        <div class="layui-input-inline">
                            <input type="checkbox" @bind="_info.IsEnable" name="open">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-md6">
        <div class="layui-card">
            <div class="layui-card-header">传给客户端的参数</div>
            <div class="layui-card-body layui-row layui-col-space10">
                <div class="layui-form-item">
                    <textarea @bind="_info.DataString" placeholder="Json格式" class="layui-textarea" style="margin-top: 0; margin-bottom: 0; height: 400px;"></textarea>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="layui-footer" style="left: 0;">
    <button class="layui-btn" @onclick="AddInfo">添加</button>
</div>

@code{

    private readonly TaskGroupVO _info = new()
    {
        IsEnable = true,
        StartAt = DateTime.Now,
        Data = new()
    };

    private async Task AddInfo()
    {
        try
        {
            // 是否为数字
            if (!IsType.IsInt(_info.Cron) && new Cron().Parse(_info.Cron))
            {
                throw new Exception("Cron格式错误");
            }
            
            var result = await _iocManager.Resolve<IFssApi>().AddTaskGroupAsync(storage,_info);
            if (result.Status)
            {
                await js.InvokeVoidAsync("layer.msg", "添加成功", new
                {
                    icon = 1
                });
                nav.NavigateTo("/fss/task_group/list");
            }
            else
            {
                await js.InvokeVoidAsync("layer.alert", result.StatusMessage, new
                {
                    icon = 2,
                    title = "出错了"
                });
            }

        }
        catch (Exception e)
        {
            await js.InvokeVoidAsync("layer.alert", e.Message, new
            {
                icon = 2,
                title = "出错了"
            });
        }
    }

    private void GotoList()
    {
        nav.NavigateTo("/fss/task_group/list");
    }

}